/**
 * @PROJECT 
 * @DATE 2018年7月21日 下午3:31:44
 * @AUTHOR LUYU
 */
package com.kool.evowkf.action.approve;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kool.core.base.IBaseAction;
import com.kool.core.exception.AppException;
import com.kool.core.util.BeanUtils;
import com.kool.evowkf.bean.SyWkfEntityBean;
import com.kool.evowkf.bean.io.approve.CMADDAPRENTITYXBean;
import com.kool.evowkf.bean.io.approve.CMADDAPRNODEXBean;
import com.kool.evowkf.bean.io.approve.CMADDAPRWORKERXBean;
import com.kool.evowkf.service.ApproveService;

/**
 * @DESCRIBE 增加审批流实例
 * @AUTHOR LUYU
 * @DATE 2018年7月21日 下午3:31:44
 *
 */
@Controller
public class AddApproveEntityAction implements IBaseAction {
	@Autowired
	private ApproveService service;

	/**
	 * @DESCRIBE
	 * @DATE 2018年7月21日 下午3:32:06
	 *
	 * @param sInput
	 * @return
	 * @throws AppException
	 */
	@Override
	@ResponseBody
	@RequestMapping(value = "/comm/wkf/addApproveEntity", method = RequestMethod.POST)
	public JSONObject doAction(@RequestBody String sInput) throws AppException {
		JSONObject pkgIn = JSONObject.parseObject(sInput);
		JSONArray secInEnt = pkgIn.getJSONArray("CMADDAPRENTITYX");
		JSONObject propInEnt = secInEnt.getJSONObject(0);
		CMADDAPRENTITYXBean ioEnt = JSON.toJavaObject(propInEnt, CMADDAPRENTITYXBean.class);
		SyWkfEntityBean entity = (SyWkfEntityBean) BeanUtils.io2Bean(ioEnt, SyWkfEntityBean.class, "swe");

		List<CMADDAPRNODEXBean> listNodes = new ArrayList<>();
		JSONArray secInNodes = pkgIn.getJSONArray("CMADDAPRNODEX");
		for (Object object : secInNodes) {
			JSONObject propInNode = (JSONObject) object;
			CMADDAPRNODEXBean ioNode = JSON.toJavaObject(propInNode, CMADDAPRNODEXBean.class);
			listNodes.add(ioNode);
		}

		List<CMADDAPRWORKERXBean> listWorkers = new ArrayList<>();
		JSONArray secInWorkers = pkgIn.getJSONArray("CMADDAPRWORKERX");
		for (Object object : secInWorkers) {
			JSONObject propInWorker = (JSONObject) object;
			CMADDAPRWORKERXBean ioWorker = JSON.toJavaObject(propInWorker, CMADDAPRWORKERXBean.class);
			listWorkers.add(ioWorker);
		}
		service.addApproveEntity(entity, listNodes, listWorkers);

		return null;
	}

}
